//  service/category.js
"use strict";
const Service = require("egg").Service;
class CategoryService extends Service {
  // 创建分类
  async newCategory(data) {
    const { app } = this;
    try {
      const result = await app.mysql.insert("category", data);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 删除分类
  async deleteCategory(id) {
    const { app } = this;
    try {
      const result = await app.mysql.delete("category", { id: id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 更新分类
  async updateCategory(data) {
    const { app } = this;
    try {
      const result = await app.mysql.update("category", data);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 分类列表
  async getCategoryList(data) {
    const { app } = this;
    try {
      let sql = "SELECT * FROM category";
      const content = []; // 参数
      let isMore = false; // 是否有多个查询参数,只用于搜索查询
      if (data.keyword) {
        sql += " WHERE CONCAT(id,name) like ?";
        content.push("%" + data.keyword + "%");
        isMore = true;
      }
      if (data.status != "" && data.status != null && data.status != undefined) {
        if (isMore) {
          // true代表有多个参数
          sql += "AND status = ?";
        } else {
          sql += " WHERE status = ?";
        }
        content.push(data.status);
        isMore = true;
      }
      if (data.begin && data.end) {
        if (isMore) {
          sql += "AND create_time BETWEEN ? AND ?";
        } else {
          sql += " WHERE create_time BETWEEN ? AND ?";
        }
        content.push(data.begin, data.end);
        isMore = true;
      }
      if (Number(data.deleted) != 1) {
        if (isMore) {
          sql += "AND deleted = 0"; 
        } else {
          sql += " WHERE deleted = 0";
        }
        isMore = true;
      }
      // 开启分页
      if (data.pageNo && data.pageSize) {
        const pageNo = data.pageNo; // 当前页码
        const pageSize = data.pageSize; // 一页展示多少条数据
        sql += " LIMIT ?,?";
        content.push(parseInt((pageNo - 1) * pageSize), parseInt(pageSize));
      }
      const res = {
        code: 200,
        page: data.pageNo,
        pageSize: data.pageSize,
        data: [],
        total: "",
      };
      this.ctx.logger.info("sql", sql);
      res.data = await this.app.mysql.query(sql, content);
      if(Number(data.deleted) != 1){
        let result = await this.app.mysql.query("SELECT COUNT(*) as total FROM category WHERE deleted = 0");
        res.total = result[0].total
      }else{
        res.total = await this.app.mysql.count("category");
      }
      return res;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 分类详情
  async getCategoryDetail(id) {
    const { app } = this;
    try {
      const result = await app.mysql.get("category", { id: id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}
module.exports = CategoryService;
